package 系统复习.链表;

import datastructure.ListNode;

/**
 * 给你一个链表，删除链表的倒数第 n 个结点，并且返回链表的头结点。
 */

public class L19 {

    public static ListNode removeNthFromEnd(ListNode head, int n) {

        ListNode cur = new ListNode();
        cur.next = head;
        ListNode tail = cur;
        ListNode temp = cur;
        for (int i = 0; i < n; i++) {
            tail = tail.next;
        }

        while (tail.next != null) {
            temp = temp.next;
            tail = tail.next;
        }
        temp.next = temp.next.next;

        return cur.next;
    }


    public static void main(String[] args) {
        ListNode listNode = removeNthFromEnd(ListNode.buildList(new int[]{1}), 1);
        ListNode.printListNode(listNode);
    }
}
